package com.harassment.interception.interception.telephone;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

import com.harassment.interception.base.HIApplication;
import com.harassment.interception.util.DateFormat;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import io.reactivex.Completable;
import io.reactivex.CompletableEmitter;
import io.reactivex.CompletableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class TelephoneRecord {
    private static final String TAG = "TelephoneRecord";
    private MediaRecorder mMediaRecorder;
    private boolean mRecord;
    private File mAudioFile;

    public TelephoneRecord() {
        mMediaRecorder = new MediaRecorder();
        mAudioFile = getPath();
        mRecord = false;
//        mAudioFile = new File(getCacheDir(), "recoder" + "_" + System.currentTimeMillis() + ".3gp");
        HIApplication.getContext().getExternalFilesDir("");
    }

    @SuppressLint("CheckResult")
    public void telephoneRecord(String number) {
        Log.d(TAG, "telephoneRecord: 开始录音方法");
        if (mRecord) {
            return;
        }
        Log.d(TAG, "telephoneRecord: thread:" + Thread.currentThread().getName());
        String outputName = number + "_" + DateFormat.format(new Date(System.currentTimeMillis())) + ".3gp";
        File outputPath = new File(mAudioFile, outputName);
        Log.d(TAG, "telephoneRecord: path:" + mAudioFile.getAbsolutePath());
        Log.d(TAG, "telephoneRecord: path:" + outputPath.getAbsolutePath());
        Log.d(TAG, "telephoneRecord: number path:" + number);
        mMediaRecorder.reset();
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//从麦克风采集声音
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //内容输出格式
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        mMediaRecorder.setOutputFile(outputPath.getAbsolutePath());//输出文件流

        try {
            mMediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "subscribe: 开始录音");
        mMediaRecorder.start();
        Log.d(TAG, "telephoneRecord: 录音中。。。");
        mRecord = true;
    }

    public void stopTelephoneRecord() {
        Log.d(TAG, "stopTelephoneRecord: 停止录音");
        if (mRecord) {
            mMediaRecorder.stop(); //停止刻录
            mMediaRecorder.release(); //释放资源
            mRecord = false;
        }
    }

    public static File getPath() {
        Context context = HIApplication.getContext();
        File file = context.getExternalFilesDir("");
        if (file == null) {
            file = context.getFilesDir();
        }
        return file;
    }

}
